package com.lce.controller;

import com.lce.beans.Leave;
import com.lce.beans.LeaveType;
import com.lce.beans.User;
import com.lce.service.ManagerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class ManagerController {

    private ManagerService managerService;

    @Resource
    public void setManagerService(ManagerService managerService) {
        this.managerService = managerService;
    }

    @RequestMapping("/getStaff")
    public Object getStaff(HttpServletRequest request) {
        HttpSession session = request.getSession();

        User user = (User) session.getAttribute("user");
        String uname = user.getUname();

        int did = managerService.getDidByUname(uname);

        List<User> users = managerService.getStaff(did);
        request.setAttribute("users", users);
        return "getStaff";
    }


    @RequestMapping("/addLeave")
    public String addLeave( HttpServletRequest request) {
        Leave leave = new Leave();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        leave.setUid(user.getUid());
        String leavetypeid = request.getParameter("leavetypeid");
        System.out.println(leavetypeid);
        String starttime = request.getParameter("starttime");
        System.out.println(starttime);
        String endtime = request.getParameter("endtime");
        System.out.println(endtime);
        String total = request.getParameter("total");
        System.out.println(total);

        leave.setTypeId(Integer.parseInt(leavetypeid));
        leave.setStartTime(starttime);
        leave.setEndTime(endtime);
        leave.setTotal(Integer.parseInt(total));

        leave.setStatus("未审批");
        leave.setApprovalId("0");


        Date date = new Date();
        SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
        String applyTime = sm.format(date);
        System.out.println(applyTime);
        leave.setApplyTime(applyTime);


        managerService.addLeave(leave);
        return "getLeaves";
    }

    @RequestMapping("/getLeaveType")
    @ResponseBody
    public Object getLeaveType(HttpServletRequest request) {
        List<LeaveType> leaveTypes = managerService.getLeaveType();
        return leaveTypes;
    }
}
